class MenusController < ApplicationController
  before_filter :find_menu_item,  :only => [:update,  :destroy]
  before_filter :require_admin

  def index
    @menu_items = Menu.find(:all)
    @menu_item  = Menu.new
  end

  def show
  end

  def new
    @menu_item = Menu.new
  end

  def create
    @menu_item = Menu.new(params[:menu])
    @menu_item.save!
    redirect_to settings_url
    flash[:update] = 'The menu item has been added.'
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def edit
  end

  def update
    @menu_item.update_attributes!(params[:menu])
    flash[:update] = 'Menu item has been changed.'      
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def destroy
    @menu_item.destroy
    flash[:update] = 'The menu item has been deleted.'
    redirect_to settings_url
  end

protected
  def require_admin
    if logged_in?
      if !current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_menu_item
    @menu_item = Menu.find params[:id]
  end
end
